﻿
using System;
using System.Drawing;
using System.Windows.Forms;

namespace SwParaPlatform2
{
    public class dlgImageViewer : Form
    {
        private PictureBox pictureBox;
        private Image image;
        private float zoomFactor = 1.0f;

        public dlgImageViewer(string imagePath)
        {
            // 初始化无边框窗口
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.BackColor = Color.Black;

            // 加载图片
            image = Image.FromFile(imagePath);

            pictureBox = new PictureBox
            {
                Image = image,
                SizeMode = PictureBoxSizeMode.Zoom, // 让图片自动适应窗口
                Dock = DockStyle.Fill, // 图片填充整个窗口
            };

            // 将 PictureBox 添加到窗体
            this.Controls.Add(pictureBox);

            // 事件处理：点击关闭、滚轮缩放
            pictureBox.MouseClick += PictureBox_MouseClick;
            this.MouseWheel += ImageViewer_MouseWheel;
            this.KeyDown += ImageViewer_KeyDown; // 按 ESC 关闭
        }

        private void PictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Close(); // 点击图片关闭窗口
            }
        }

        private void ImageViewer_MouseWheel(object sender, MouseEventArgs e)
        {
            // 计算缩放因子
            if (e.Delta > 0)
            {
                zoomFactor += 0.05f;
            }
            else if (e.Delta < 0 && zoomFactor > 0.05f)
            {
                zoomFactor -= 0.05f;
            }

            // 根据缩放因子更新窗口大小（图片随窗口一起缩放）
            this.Width = (int)(image.Width * zoomFactor);
            this.Height = (int)(image.Height * zoomFactor);

            // 窗口位置调整，始终居中
            this.CenterToScreen();
        }

        private void ImageViewer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close(); // 按下 ESC 关闭窗口
            }
        }
    }
}
